﻿$(function () {
    $('#navigation li').removeClass('active');
    $('#menu_auditedList').addClass('active');
    twiSelf.init();
});

Twi.SCS.AuditedList = {
    Vars: {
        startIndex: 0,
        pageSize: 5,
        FTimeSection: 2, //2上午；3下午
        FTimeSection2: 3,
        typeAddTypeID: null, //添加记录类型的ID
        typeAddTypeName: null
    }
    , init: function () {
        twiSelf.initDate();
        twiSelf.initQueryType();
        $('.typebar_actionIcon').click(function () {
            var optionID = $(this).attr("optionID");
            if (optionID) {
                $('#query_type option[value="' + optionID + '"]').attr('selected', true);
                twiSelf.queryAction();
            }
        });

        $('.type_add').click(function () {
            $('#noetbook_errorMsg').hide();
            $('#pop_win').show();
            $('#pop_win .notebook_head span').html($(this).attr("title") + "单据录入");
            twiVars.typeAddTypeID = $(this).attr('optionID');
            twiVars.typeAddTypeName = $(this).attr("title");
        });

        //toggle 动画
        $('#toggle_start').toggle(function () {
            $(this).children(".toggle_start_img").animate({
                "background-position": "100%"
            });
            twiVars.FTimeSection = 3;
        }, function () {
            $(this).children(".toggle_start_img").animate({
                "background-position": "0%"
            });
            twiVars.FTimeSection = 2;
        });
        $('#toggle_end').toggle(function () {
            $(this).children(".toggle_end_img").animate({
                "background-position": "0%"
            });
            twiVars.FTimeSection2 = 2;
        }, function () {
            $(this).children(".toggle_end_img").animate({
                "background-position": "100%"
            });
            twiVars.FTimeSection = 3;
        });

        //pow_win  关闭
        $('#pop_win .notebook_close').click(function () {
            $('#pop_win').hide();
        });

        //提交记录
        $('#notebook_submit').click(function () {
            twiSelf.notebook_submit();
        });

        $('#query').click(function () {
            twiSelf.queryAction();
        });

        //修改详情窗口
        $("#dialogRecordDetail").dialog({
            autoOpen: false,
            show: "blind",
            hide: "clip",
            width: 400,
            minHeight: 140,
            modal: true,
            buttons: [{
                text: '保存',
                click: function () {
                    var newDetail = $('#dialogRecordDetail textarea').val();
                    var id = $('#dialogRecordDetail input[type="hidden"]').first().val();
                    Twi.Ajax({
                        data:
                        {
                            cn: 'OutRecord',
                            mn2: 'EditDetail',
                            id: id,
                            newDetail: newDetail
                        },
                        success: function (twi) {
                            $('#' + id).find('.record_itm_detail').first().html(newDetail);
                            $('#dialogRecordDetail').dialog("close");
                        }
                    })
                }
            }, {
                text: '取消',
                click: function () {
                    $(this).dialog("close");
                }
            }]
        });

        twiSelf.queryAction();
    }

    , queryAction: function () {
        Twi.Ajax({
            data: {
                cn: 'OutRecord',
                mn2: 'GetByCondition',
                startIndex: twiVars.startIndex,
                pageSize: twiVars.pageSize,
                beginDate: $('#query_startDate').val(),
                endDate: $('#query_endDate').val(),
                keyValues: JSON.stringify({
                    FUserRealName: $('#query_userName').val(),
                    FTypeID: $('#query_type').val()
                })
            },
            success: function (twi) {
                $('#record_list').html('');
                var list = JSON.parse(twi.data);
                for (var i = 0; i < list.length; i++) {
                    var itm = list[i];
                    var iconPicName = "LaptopBag0099FF.png";
                    if (itm.FTypeID == "c4acf5b5-f598-4807-9445-fc8e0cf48cd3") //出差
                    {
                        iconPicName = "LaptopBag0099FF.png";
                    }
                    else if (itm.FTypeID == "b2f20a3b-8d6f-4515-8644-3b0522074c1a")//请假
                    {
                        iconPicName = "HospitalFDB813.png";
                    }
                    else if (itm.FTypeID == "1f1d20cb-31d0-4537-b264-9144102c155f")//临时外出
                    {
                        iconPicName = "BusinessMan85C404.png";
                    }
                    else if (itm.FTypeID == "6a98e085-ca23-4ea8-ba79-86710bf87fa9")//调休
                    {
                        iconPicName = "Smile9E149E.png";
                    }
                    var betweenDateTime = "";
                    if (itm.FStartDate == itm.FEndDate) {
                        betweenDateTime = itm.FStartDate + " " + twiSelf.timeSectionRenderer(itm.FTimeSection);
                    }
                    else {
                        betweenDateTime = itm.FStartDate + " " + twiSelf.timeSectionRenderer(itm.FTimeSection) + " ~ " + itm.FEndDate + " " + twiSelf.timeSectionRenderer(itm.FTimeSection2);
                    }
                    var html = '<div id="' + itm.FID + '" class="record_itm clear">';
                    html += '<div class="record_type_icon">';
                    html += '<img src="css/images/' + iconPicName + '" alt="" />';
                    html += '<div class="record_type_icon_name">' + itm.FTypeName + '</div>';
                    html += '</div>';
                    html += ' <div class="record_itm_con">';
                    html += '<div class="record_itm_box">';
                    html += '<div class="record_itm_title">' + itm.FUserRealNames + '<span class="separtor">|</span>' + itm.FTitle + '</div>';
                    html += '<div>' + betweenDateTime + '</div>';
                    html += '<div class="record_itm_detail">' + itm.FDetail + '</div>';
                    html += '<div class="record_itm_pubInfo">';
                    html += '<div class="left">' + '审批人/日期：' + itm.FAuditor + ' ' + itm.FAuditDate + '</div>';
                    html += '<div class="left">' + '<span class="separtor">|</span>' + '录入人/时间：' + itm.FOperator + ' ' + itm.FOperateDateTime + '</div>';
                    html += '<div class="action_box" outRecordID="' + itm.FID + '">';
                    html += '<a onClick=twiSelf.editRecord("' + itm.FID + '")>编辑</a>';
                    html += '<span class="separtor">|</span>';
                    html += '<a onClick=twiSelf.deleteByOutRecordId("' + itm.FID + '")>删除</a>';
                    //html += '<span class="separtor">|</span>';
                    //html += '<a>上传记录单</a>';
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="record_itm_arr ta_out">◥</div>';
                    html += '<div class="record_itm_arr ta_inner">◥</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="cl">&nbsp;</div>';
                    $('#record_list').append(html);
                }
            }
        });
    }

    , deleteByOutRecordId: function (outRecordId) {
        Twi.Ajax({
            data:
            {
                cn: 'OutRecord',
                mn2: 'Delete',
                outRecordId: outRecordId
            },
            success: function (twi) {
                $('#' + outRecordId).remove();
            }
        });
    }

    , editRecord: function (outRecordId) {
        $('#dialogRecordDetail textarea').val("");
        Twi.Ajax({
            data:
            {
                cn: 'OutRecord',
                mn2: 'GetEntityByID',
                id: outRecordId
            },
            success: function (twi) {
                $('#dialogRecordDetail').dialog('open');
                var entity = JSON.parse(twi.data);
                $('#dialogRecordDetail textarea').val(entity.FDetail);
                $('#dialogRecordDetail input[type="hidden"]').first().val(outRecordId);
            }
        });
    }

    , timeSectionRenderer: function (timeSection) {
        if (timeSection == 1) {
            return "全天";
        }
        else if (timeSection == 2) {
            return "上午";
        }
        else if (timeSection == 3) {
            return "下午";
        }
        return timeSection;
    }
    , initDate: function () {
        //开始日期、结束日期
        $("#query_startDate").datepicker({
            changeMonth: true,
            altFormat: "yy-mm-dd",
            dateFormat: "yy-mm-dd",
            maxDate: new Date(),
            onSelect: function (dateText, inst) {
                $("#query_endDate").datepicker("option", "minDate", dateText);
            }
        });
        $("#query_endDate").datepicker({
            changeMonth: true,
            altFormat: "yy-mm-dd",
            dateFormat: "yy-mm-dd",
            maxDate: new Date(),
            onSelect: function (dateText, inst) {
                $("#query_startDate").datepicker("option", "maxDate", dateText);
            }
        });
        var tDay = new Date();
        tDay.setMonth(tDay.getMonth() - 1);
        $("#query_startDate").datepicker('setDate', tDay);
        $("#query_endDate").datepicker('setDate', new Date());

        //notebook 开始日期、结束日期
        $("#notebook_startDate").datepicker({
            changeMonth: true,
            altFormat: "yy-mm-dd",
            dateFormat: "yy-mm-dd",
            onSelect: function (dateText, inst) {
                $("#notebook_endDate").datepicker("option", "minDate", dateText);
            }
        });
        $("#notebook_endDate").datepicker({
            changeMonth: true,
            altFormat: "yy-mm-dd",
            dateFormat: "yy-mm-dd",
            onSelect: function (dateText, inst) {
                $("#notebook_startDate").datepicker("option", "maxDate", dateText);
            }
        });
        $("#notebook_startDate").datepicker('setDate', new Date());
        $("#notebook_endDate").datepicker('setDate', new Date());
        $('#notebook_auditDate').datepicker({
            changeMonth: true,
            altFormat: "yy-mm-dd",
            dateFormat: "yy-mm-dd"
        });
        $("#notebook_auditDate").datepicker('setDate', new Date());
    }

    , initQueryType: function () {
        Twi.Ajax({
            data:
             {
                 cn: 'Option',
                 mn2: 'GetByOptionTypeID',
                 optionTypeID: '8602E798-46B8-4AC0-A938-2F21A1AF4487'
             },
            success: function (twi) {
                if (twi.totalCount > 0) {
                    var list = JSON.parse(twi.data);
                    for (var i = 0; i < list.length; i++) {
                        var itm = list[i];
                        $('#query_type').append(' <option value="' + itm.FID + '">' + itm.FName + '</option>');
                    }
                }
            }
        });
    }

    , validateNotebookData: function () {
        if ($('#notebook_title').val().length == 0) {
            $('#noetbook_errorMsg').html('请填写标题');
            $('#noetbook_errorMsg').show();
            return false;
        }
        if ($('#notebook_users').combobox('getValues').length == 0) {
            $('#noetbook_errorMsg').html('请选择人员');
            $('#noetbook_errorMsg').show();
            return false;
        }
        if ($('#notebook_auditors').combobox('getValue') == "") {
            $('#noetbook_errorMsg').html('请选择审批人');
            $('#noetbook_errorMsg').show();
            return false;
        }
        if ($('#notebook_auditDate').val().length == 0) {
            $('#noetbook_errorMsg').html('请选择审批日期');
            $('#noetbook_errorMsg').show();
            return false;
        }
        $('#noetbook_errorMsg').hide();
        return true;
    }

    , notebook_submit: function () {
        if (!twiSelf.validateNotebookData()) {
            return;
        }
        var data = {
            FTypeID: twiVars.typeAddTypeID,
            FTypeName: twiVars.typeAddTypeName,
            FTitle: $('#notebook_title').val(),
            FUserRealNames: $('#notebook_users').combobox('getText'),
            UserIDs: $('#notebook_users').combobox('getValues').join(','),
            FStartDate: $('#notebook_startDate').val(),
            FEndDate: $('#notebook_endDate').val(),
            FTimeSection: twiVars.FTimeSection,
            FTimeSection2: twiVars.FTimeSection2,
            FAuditor: $('#notebook_auditors').combobox('getText'),
            FAuditorID: $('#notebook_auditors').combobox('getValue'),
            FAuditDate: $('#notebook_auditDate').val(),
            FDetail: $('#notebook_detail').val()
        };
        Twi.Ajax({
            data:
            {
                cn: 'OutRecord',
                mn2: 'NewRecord',
                data: JSON.stringify(data)
            },
            success: function (twi) {
                $('#pop_win').hide();
                alert("新增成功");
            }
        });
    }
};

var twiSelf = Twi.SCS.AuditedList;
var twiVars = twiSelf.Vars;